package net.sareweb.acab.actions.dialog;

import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.faces.event.ActionEvent;

import net.sareweb.acab.components.session.AcabIdentity;
import net.sareweb.acab.entity.Comment;
import net.sareweb.acab.entity.Dialog;
import net.sareweb.acab.entity.Seat;
import net.sareweb.acab.entity.Travel;
import net.sareweb.acab.entity.User;
import net.sareweb.acab.entity.manager.CommentManager;
import net.sareweb.acab.entity.manager.DialogManager;
import net.sareweb.acab.entity.manager.SeatManager;
import net.sareweb.acab.entity.manager.TravelManager;
import net.sareweb.acab.entity.manager.UserManager;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.core.Events;
import org.jboss.seam.framework.EntityQuery;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Credentials;

@Name("travelDialogsBean")

@Scope(ScopeType.CONVERSATION)
public class TravelDialogsBean extends EntityQuery<Dialog> {
	
	@Logger
	private Log log;

	@In(create=true)
	CommentManager commentManager;
	@In(create=true)
	DialogManager dialogManager;
	@In(create=true)
	SeatManager seatManager;
	@In(create=true)
	UserManager userManager;
	@In(create=true)
	TravelManager travelManager;
	@In 
    AcabIdentity identity;
	
	private static final String EJBQL = "select dialog from Dialog dialog";
	private static final String[] RESTRICTIONS = {
		"dialog.travel.idTravel = #{travelDialogsBean.idTravel}",
		};
	

	private Integer idTravel;
	public Integer getIdTravel() {
		return idTravel;
	}

	public void setIdTravel(Integer idTravel) {
		this.idTravel = idTravel;
	}

	private Travel travel = new Travel();
	private Integer selectedDialogId;
	private List<Comment> dialogComments;
	private String newMessage;
	
	
	/*data for asigning seats*/
	private Integer idSeat;
	private Integer idUser;
	private String  travelName;
	private String  userName;
	
	public TravelDialogsBean() {
		setEjbql(EJBQL);
		setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
		setMaxResults(25);
		
		if(travel.getIdTravel()!=null){
			travel = travelManager.find(travel.getIdTravel());
			log.debug("Loaded travel:" + travel.getIdTravel());
		}
	}

	public Travel getTravel() {
		if(travel.getName()==null){ //si no hemos cargado el travel de la base de datos lo obtenemos
			log.debug("going to load travel:" + idTravel);
			travel = travelManager.find(idTravel);
		}
		return travel;
	}
	
	public Integer getSelectedDialogId() {
		return selectedDialogId;
	}

	public void setSelectedDialogId(Integer selectedDialogId) {
		this.selectedDialogId = selectedDialogId;
	}
	
	public List<Comment> getDialogComments(){
		if(selectedDialogId == null) return null;
		return dialogComments;
	}
	
	public void selectDialogActionListener(ActionEvent event){
		Dialog d = dialogManager.find(selectedDialogId);
		d.setOwnerSeenDate(new Date());
		dialogManager.save(d);
		updateSelectedDialogComments();
	}
	
	public void ownerSendMessageActionListener(ActionEvent event){
		log.debug("writing new message {0} by {1} to dialog {2}", newMessage, identity.getUser().getLogin(),selectedDialogId);
		Dialog d =  dialogManager.find(selectedDialogId);
		Comment c = new Comment();
		c.setCreateDate(new Date());
		c.setDialog(d);
		c.setMsg(newMessage);
		c.setUser(identity.getUser());
		commentManager.save(c);
		d.setLastCommentDate(new Date());
		d.setOwnerSeenDate(new Date());
		dialogManager.save(d);
		newMessage="";
		updateSelectedDialogComments();
	}
	
	public void asignSeatToUserActionListener(ActionEvent event){
		log.debug("asigning seat to user");
		Seat s = seatManager.find(idSeat);
		User u = userManager.find(idUser);
		s.setUser(u);
		s.setState(SeatManager.SEAT_STATE_OCCUPIED);
		seatManager.save(s);
	}
	
	public String asignSeatToUserAction(Integer sId, Integer uId){
		log.debug("asigning seat {0} to user {1}", sId, uId);
		Seat s = seatManager.find(sId);
		User u = userManager.find(uId);
		s.setUser(u);
		s.setState(SeatManager.SEAT_STATE_OCCUPIED);
		seatManager.save(s);
		Events.instance().raiseEvent("seatAsignedEvent");
		return "refresh";
	}
	
	public String releaseByUserAction(Integer uId){
		log.debug("Releasing seat by user id {0} in travel {1}", uId, travel.getIdTravel());
		seatManager.releaseSeatByTravelAndUser(travel.getIdTravel(), uId);
		Events.instance().raiseEvent("seatReleasedEvent");
		return "refresh";
	}
	
	public boolean userHasSeatInTravel(Integer idUser){
		Iterator<Seat> seatIterator = travel.getSeats().iterator();
		while(seatIterator.hasNext()){
			Seat s =seatIterator.next(); 
			if(s.getState().equals(SeatManager.SEAT_STATE_OCCUPIED) && s.getUser()!=null && s.getUser().getIdUser().equals(idUser))return true;
		}
		return false;
	}
	
	
	
	private void updateSelectedDialogComments(){
		dialogComments = commentManager.getDialogComments(selectedDialogId);
	}

	public String getNewMessage() {
		return newMessage;
	}

	public void setNewMessage(String newMessage) {
		this.newMessage = newMessage;
	}

	public Integer getIdSeat() {
		return idSeat;
	}

	public void setIdSeat(Integer idSeat) {
		this.idSeat = idSeat;
	}

	public Integer getIdUser() {
		return idUser;
	}

	public void setIdUser(Integer idUser) {
		this.idUser = idUser;
	}

	public String getTravelName() {
		return travelName;
	}

	public void setTravelName(String travelName) {
		this.travelName = travelName;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	
}
